<?php
class PHZ_sort
{
    /**
     * 胡牌，牌排序； 只需排序手中的即可；如果胡的牌刚好是畏的牌或者是提(非跑，跑胡就不是自摸了)的牌；则证明是自摸；这个时候就不需要把胡的牌加入到手牌中了；否则需要加入到手牌中、
     * 已经xp的牌，肯定是成坎的；只需要把顺序重置下即可了。
     * @param $sp           手牌
     * @param $card_info    胡牌人牌面信息
     * @param $card_number  胡牌牌号
     */
    public static function __lipaiSort( $sp , $card_info , $card_number ){
        $card_data = array();
        //当前xp中有多少个4个牌的组合？  PS:手牌中不可能有4个一样的，只有下牌中才会有
        $four_card = 0;
        //首先将xp置入到data数组中 - 并判断xp中是否有4个跑牌
        $xp_array  = explode(",", $card_info['xp'] );
        foreach ( $xp_array as $key => $value ){
            if( $value != ""){array_push( $card_data , $value );if( substr_count( $value , "_" ) >= 3 ){ $four_card++; }}
        }
        //是否有手牌，没有手牌直接返回
        if( trim( $sp ) == "" || $sp == "," ){
            return self::__endDataSort( $card_data );
        }
        //是否胡的牌处于畏或者提里面 - 如果都不处于，则需要将胡的牌，置入到手牌中
        $card_ti   = $card_info['card_ti'];
        $card_wei  = $card_info['card_wei'];
        if( strstr( $card_ti , (string)$card_number ) == false && strstr( $card_wei , (string)$card_number ) == false ){
            $sp = $sp . "," . $card_number;
        }
        //将牌信息按照逗号进行分割开来
        $card_array       = explode(",",$sp);
        $card_count_array = array_count_values( $card_array );
        //删除3牌并添加为一坎
        foreach ( $card_count_array as $card => $count ){
            if( $count == 3 ){
                array_push( $card_data , $card."_".$card."_".$card );
                foreach ( $card_array as $key => $value ){ if( $value == $card ){ unset( $card_array[$key] ); } }//删除这个3个牌的牌
                sort( $card_array );//重置排序
            }
        }
        //如果存在4个牌，不管是1墩还是2墩；那么手中都必须有2个相同的;这里累计有多少个对子；如果只有1个对子；那这里就去掉；如果有2个以上的对子；那这里就不能去
        $is_dui = 0;$dui_card = 0;
        if( $four_card >= 1 ){
            foreach ( $card_count_array as $card => $count ){
                if( $count == 2 ){
                    $is_dui++;
                    $dui_card = $card;
                }
            }
            if( $is_dui == 1 ){
                array_push( $card_data , $dui_card."_".$dui_card );
                foreach ( $card_array as $key => $value ){ if( $value == $dui_card ){ unset( $card_array[$key] ); } }//删除这个2个牌的牌
                sort( $card_array );//重置排序
            }
        }
        //经过上面的牌面处理,牌面肯定是3个3个成墩的牌
        $find  = false;
        $count = count( $card_array );#.还剩余牌的数量
        if( $count == 3 ){//如果只剩3张牌了,那么直接把这3张牌置入进去就可以了
            array_push( $card_data , $card_array[0] . "_" . $card_array[1] . "_" . $card_array[2] );
            $find      = true;
            $card_data = self::__endDataSort( $card_data );
        }else{//大于等4张；也就是说至少还有2墩的牌；而且这2墩都是3余数的牌
            //搜遍4种走法；如果有能走完的，则拼接，所有都不能走完；则输出false
            $all_array = array(
                array(1,2,3,4),array(1,2,4,3),array(1,3,2,4),array(1,3,4,2),array(1,4,3,2),array(1,4,2,3),
                array(2,1,3,4),array(2,1,4,3),array(2,3,1,4),array(2,3,4,1),array(2,4,1,3),array(2,4,3,1),
                array(3,1,2,4),array(3,1,4,2),array(3,2,4,1),array(3,2,1,4),array(3,4,2,1),array(3,4,1,2),
                array(4,3,2,1),array(4,3,1,2),array(4,2,3,1),array(4,2,1,3),array(4,1,2,3),array(4,1,3,2),
            );
            foreach ( $all_array as $key => $value ){
                $temp_card_array = $card_array;
                $temp_card_data  = $card_data;
                $data = self::__endShunzi( $temp_card_array , $temp_card_data , $value );
                //如果本次应该有对子，但是对子出现了大于等于2对的情况下；这里刚好拆的只剩一个对子了，那么去掉这个对子
                if( $four_card >= 1 && $is_dui >= 2 && count( $data[0] ) == 2 ){
                    $two_card = $data[0];sort( $two_card );
                    if( $two_card[0] == $two_card[1] ) {
                        $data[0] = array();
                        array_push( $data[1] , $two_card[0] . "_" . $two_card[1]);
                    }
                }
                //是否已经去除了所有的牌
                if( count( $data[0] ) <= 0 ){
                    $find = true;
                    $card_data = self::__endDataSort( $data[1] );
                    break;
                }
            }
        }
        if( $find == true ){
            return $card_data;
        }
        return false;
    }

    private static function __endDataSort( $card_data ){
        foreach ( $card_data as $key => $value ){
            $value_array     = explode("_",$value);asort($value_array);sort( $value_array );
            $card_data[$key] = $value_array;
        }
        return $card_data;
    }

    private static function __endShunzi( $card_array , $card_data , $diss ){
        for ( $i = 0 ; $i <= 3 ; $i++ ) {
            //顺子 - 1
            if( $diss[$i] == 1 ) {
                $shunzi_array = array(
                    array(1,2,3),array(2,3,4),array(3,4,5),array(4,5,6),array(5,6,7),array(6,7,8),array(7,8,9),array(8,9,10),
                    array(11,12,13),array(12,13,14),array(13,14,15),array(14,15,16),array(15,16,17),array(16,17,18),array(17,18,19),array(18,19,20),
                );
                foreach ( $shunzi_array as $key_1 => $value_1 ){
                    $data = self::__number_3( $value_1[0],$value_1[1],$value_1[2] , $card_array , $card_data );$card_array = $data[0];$card_data  = $data[1];
                }
                if( count( $card_array ) <= 0 ){ return $data; }
            }
            //赖子 - 2
            if( $diss[$i] == 2 ){
                $laizi_array = array(
                    array(1,1,11),array(11,11,1),array(1,11,1),array(11,1,11),array(11,1,1),array(1,11,11),
                    array(2,2,12),array(12,12,2),array(2,12,2),array(12,2,12),array(12,2,2),array(2,12,12),
                    array(3,3,13),array(13,13,3),array(3,13,3),array(13,3,13),array(13,3,3),array(3,13,13),
                    array(4,4,14),array(14,14,4),array(4,14,4),array(14,4,14),array(14,4,4),array(4,14,14),
                    array(5,5,15),array(15,15,5),array(5,15,5),array(15,5,15),array(15,5,5),array(5,15,15),
                    array(6,6,16),array(16,16,6),array(6,16,6),array(16,6,16),array(16,6,6),array(6,16,16),
                    array(7,7,17),array(17,17,7),array(7,17,7),array(17,7,17),array(17,7,7),array(7,17,17),
                    array(8,8,18),array(18,18,8),array(8,18,8),array(18,8,18),array(18,8,8),array(8,18,18),
                    array(9,9,19),array(19,19,9),array(9,19,9),array(19,9,19),array(19,9,9),array(9,19,19),
                    array(10,10,20),array(20,20,10),array(10,20,10),array(20,10,20),array(20,10,10),array(10,20,20)
                );
                foreach ( $laizi_array as $key_1 => $value_1 ){
                    $data = self::__number_3( $value_1[0],$value_1[1],$value_1[2] , $card_array , $card_data );$card_array = $data[0];$card_data  = $data[1];
                }
                if( count( $card_array ) <= 0 ){ return $data; }
            }
            //1.5.10 - 3
            if( $diss[$i] == 3 ){
                $data = self::__number_3( 1,5,10 , $card_array , $card_data );$card_array = $data[0];$card_data  = $data[1];if( count( $card_array ) <= 0 ){ return $data; }
                $data = self::__number_3( 11,15,20 , $card_array , $card_data );$card_array = $data[0];$card_data  = $data[1];if( count( $card_array ) <= 0 ){ return $data; }
            }
            //2.7.10 - 4
            if( $diss[$i] == 4 ){
                $data = self::__number_3( 2,7,10 , $card_array , $card_data );$card_array = $data[0];$card_data  = $data[1];if( count( $card_array ) <= 0 ){ return $data; }
                $data = self::__number_3( 12,17,20 , $card_array , $card_data );$card_array = $data[0];$card_data  = $data[1];if( count( $card_array ) <= 0 ){ return $data; }
            }
        }
        //返回
        return array( $card_array , $card_data );
    }

    private static function __number_3( $n1 , $n2 , $n3 , $card_array , $card_data ){
        $all_find  = 0;
        $temp_card = $card_array;
        foreach ( $temp_card as $key => $value ){if( $value == $n1 ){unset( $temp_card[$key] );$all_find++;break;}}
        foreach ( $temp_card as $key => $value ){if( $value == $n2 ){unset( $temp_card[$key] );$all_find++;break;}}
        foreach ( $temp_card as $key => $value ){if( $value == $n3 ){unset( $temp_card[$key] );$all_find++;break;}}
        if( $all_find == 3 ){
            array_push( $card_data , $n1 . "_" . $n2 . "_" . $n3 );
            foreach ( $card_array as $key => $value ){ if( $value == $n1 ){ unset( $card_array[$key] );break; } }
            foreach ( $card_array as $key => $value ){ if( $value == $n2 ){ unset( $card_array[$key] );break; } }
            foreach ( $card_array as $key => $value ){ if( $value == $n3 ){ unset( $card_array[$key] );break; } }
        }
        return array($card_array,$card_data);
    }
}